package org.itboys.mobile.service.mongo.web;


import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayAdvertisementZone;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告位_service
 */
@Service
public class SundayAdvertisementZoneService extends BaseMongoService<SundayAdvertisementZone> {
    @Autowired
    private SundayAdvertisementService advertisementService;
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayAdvertisementZone> getEntityClass() {
        return SundayAdvertisementZone.class;
    }
    /**
     * 修改或新增
     * @param zone
     */
    public void saveZone(SundayAdvertisementZone zone){
        Map<String,Object> param =new HashMap<String, Object>();
        if(zone.getId()==0l){
            param.put("isDeleted",0);
            param.put("key",zone.getKey());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("广告位Key不能重复");
            }
            super.save(zone);
        }else{
            param.put("id !=",zone.getId());
            param.put("isDeleted",0);
            param.put("key",zone.getKey());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("广告位Key不能重复");
            }
            super.updateExceptEmpty(zone.getId(),zone);
        }
    }

    /**
     * 分页获取广告位
     * @param request
     * @return
     */
    public PageResult<SundayAdvertisementZone> select(HttpServletRequest request){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("isDeleted", 0);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"key","name");
        PageResult<SundayAdvertisementZone> result=super.containpageQuery(request,containsparam,param);
        return result;
    }
    /**
     * 删除广告位
     * @param id
     */
    public void deleteZone(Long id){

        //删除广告位
        super.delete(id);
        //删除对应的广告信息
        SundayAdvertisementZone zone= super.getById(id);
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("isDeleted", 0);
        param.put("zoneKey",zone.getKey());
        try {
            advertisementService.delete(param);
        }catch (Exception e){

            e.printStackTrace();
            logger.info("删除广告信息失败。广告位ID"+zone.getId());

            //回滚广告微信
            super.update(id,"isDeleted", SundayCommonConstants.NO);
        }



    }

}
